package com.lechuang.user.controller;

import cn.hutool.json.JSONUtil;
import com.lechuang.common.result.Result;
import com.lechuang.user.dto.UserForgetPwdDto;
import com.lechuang.user.dto.UserLoginDto;
import com.lechuang.user.dto.UserRegisterDto;
import com.lechuang.user.manager.LoginManager;
import com.lechuang.user.vo.LoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/login")
@Log4j2
@Tag(name = "用户登录类")
public class UserLoginController {


    @Resource
    private LoginManager loginManager;



    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<LoginVo> userLogin(@RequestBody UserLoginDto loginDto){
        log.info("用户登录------{}", JSONUtil.toJsonStr(loginDto));
        LoginVo loginVo = loginManager.userLogin(loginDto);
        return Result.success(loginVo);
    }

    @Operation(summary = "注册")
    @PostMapping("/register")
    public Result<LoginVo> register(@RequestBody UserRegisterDto userRegisterDto){
        log.info("用户注册------{}", JSONUtil.toJsonStr(userRegisterDto));
        LoginVo loginVo = loginManager.userRegister(userRegisterDto);
        return Result.success(loginVo);
    }

    @Operation(summary = "忘记密码")
    @PostMapping("/forgetPwd")
    public Result<LoginVo> forgetPwd(@RequestBody UserForgetPwdDto userForgetPwdDto){
        log.info("忘记密码------{}", JSONUtil.toJsonStr(userForgetPwdDto));
        loginManager.userForgetPwd(userForgetPwdDto);
        return Result.success();
    }

    @Operation(summary = "退出登录")
    @GetMapping("/logout")
    public Result logout(@RequestHeader String token){
        log.info("退出登录---->token:{}",token);
        loginManager.logout(token);
        return Result.success();
    }



}
